const request = require('request');
var express = require('express');
var router = express.Router();

//查询列出包的路由
router.use('/search/package/:pkgname', function (req, res, next) {
    var pkgname = req.params.pkgname;
    var repo = req.query.repo;

    if (pkgname && pkgname.length > 0) {
        console.log('收到一个请求：' + pkgname);
        pkgname = pkgname.toLowerCase();

        request(repo + pkgname + '/', { json: false }, (err, res1, body) => {
            if (err) { 
                return console.log(err); 
            }
            // console.log(body);
            // res.send(body);
            // var packages = body.match(/<a href="(.*)">(.*)<\/a>/g);
            var packages = [];
            var regExp = /<a href="(.*)".*>(.*)<\/a>/g;
            var onePackInfo;
            var lastVersion = '';
            var count = 0;
            while ((onePackInfo = regExp.exec(body)) != null) {
                var oneFile = onePackInfo[2];
                var fileMainName = oneFile;
                var fileType = '';
                if (oneFile.indexOf('.whl') == oneFile.length - 4) {
                    fileMainName = oneFile.substring(0, oneFile.length - 4);
                    fileType = 'whl';
                } else if (oneFile.indexOf('.tar.gz') == oneFile.length - 7) {
                    fileMainName = oneFile.substring(0, oneFile.length - 7);
                    fileType = 'tar.gz';
                }
                if (fileType == '') {
                    continue;
                }
                var fnBlocks = fileMainName.split('-');
                var pkgName = fnBlocks[0].replace(/_/g, '-');
                var version = fnBlocks[1];
                var pyVer = '';
                var abiVer = '';
                var osvers = [];
                if (fileType == 'whl') {
                    if (fnBlocks.length >= 3) {
                        pyVer = fnBlocks[2];
                    }
                    if (fnBlocks.length >= 4) {
                        abiVer = fnBlocks[3];
                    }
                    if (fnBlocks.length >= 5) {
                        osvers = fnBlocks[4].split('.');
                    }
                } else if (fileType == 'tar.gz') {
                    pkgName = pkgname;
                    version = fileMainName.substring(pkgname.length+1);
                }
                if (version != lastVersion) {
                    if (count > 1) {
                        packages.push({
                            pkgname: pkgName,
                            version: version,
                            pyver: '（全部）',
                            abiver: '',
                            osvers: [],
                            filetype: ''
                        });
                    }
                    lastVersion = version;
                    count = 0;
                }
                packages.push({
                    pkgname: pkgName,
                    version: version,
                    pyver: pyVer,
                    abiver: abiVer,
                    osvers: osvers,
                    filetype: fileType
                });
                count++;
            }
            res.send(JSON.stringify(packages.reverse()));
          });          
    } else {
        res.send('文件提取失败');
    }
});

module.exports = router;
